#include "UdpServer.hpp"
#include <memory>
#include <stdio.h>
#include <fcntl.h>
void Usage(char *proc)
{
    std::cout << proc << " serverport" << std::endl;
    exit(1);
}

std::string Ping(std::string req)
{
    return req + "-pong";
}

std::string ExecuteCommand(std::string command)
{
    FILE *fp = popen(command.c_str(), "r");
    if (!fp)
    {
        return "execute error,reason unknown";
    }
    // 将文件的内容读出来
    char buffer[1024];
    int n = read(fp->_fileno, buffer, sizeof(buffer) - 1);
    if (n <= 0)
    {
        return "execute error,reason open fail";
    }
    buffer[n] = 0;
    return buffer;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
    }

    // 创建服务器
    int serverport = std::stoi(argv[1]);
    std::unique_ptr<UdpServer> svr = std::make_unique<UdpServer>(ExecuteCommand, serverport);
    svr->Init();
    svr->Start();

    return 0;
}